/**
 * author: Maxim Dybarskiy
 * date:   Jun 22, 2011
 * time:   4:47:22 PM
 */
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import d.max.wallpaper.util.TreeSelectionListener;
import java.io.File;

public class TreeView extends ListView implements OnItemClickListener {
    
    private TreeAdapter adapter;
    private TreeSelectionListener listener;
    
    public TreeView(Context context) {
        super(context);
    }
    
    public TreeView(Context context, AttributeSet set) {
        super(context, set);
    }

    public void setListener(TreeSelectionListener listener) {
        this.listener = listener;
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
        this.adapter = (TreeAdapter) adapter;
        setOnItemClickListener(this);
    }
    
    @Override
    public void onItemClick(AdapterView<?> av, View view, int position, long id) {
        File file = (File) adapter.getFiles()[position];
        
    }
    
//    @Override
//    public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) {
//        File file = (File) adapter.getGroup(arg2);
//        if (!file.canRead()) {
//            Toast.makeText(getContext(), "No permissions!", Toast.LENGTH_SHORT).show();
//            return true;
//        }
//        listener.pathSelected(file.getAbsolutePath());
//        if (isGroupExpanded(arg2)) {
//            expand(null);
//            collapseGroup(arg2);
//        } else {
//            expand(file);
//            expandGroup(arg2);
//        }
//        return true;
//    }
    
    public void browse(File dir) {
        if (dir.isDirectory()) {
            if (dir != null && dir.isDirectory()) {
                adapter.setFiles(dir.listFiles());
            }
        } else {
            Toast.makeText(getContext(), dir.getName() + " not a directory", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void expand(File dir) {
        if (dir == null) {
            return;
        }
        if (dir.isDirectory()) {
            adapter.addExmapded(dir);
        } else {
            Toast.makeText(getContext(), dir.getName() + " not a directory", Toast.LENGTH_SHORT).show();
        }
    }
}
